package action

import (
	"gitee.com/sansaniot/ssiot-core/facade/env"
	"gitee.com/sansaniot/ssiot-core/logger"
	"gorm.io/gorm"

	"ssnotification/common/constant"
	"ssnotification/common/enums/able"
	"ssnotification/common/enums/notifysource"
	"ssnotification/common/enums/sendway"
	"ssnotification/common/utils"
	"ssnotification/internal/data/handler"
	"ssnotification/internal/notify/models"
	"ssnotification/internal/notify/models/query"
)

type Handle struct {
	Orm *gorm.DB
}

func (action *Handle) Execute(topic string, jsonData []map[string]interface{}) {
	//遍历消息集合处理每个类型
	for _, notify := range jsonData {
		//获取当前的消息类中
		source := utils.GetStringValueFiled(notify, "source")
		//获取当前的类型
		switch source {
		case notifysource.Alarm.Get().Source:
			//告警类型的消息处理
			action.handleAlarmNotify(notify)
		case notifysource.UseOperate.Get().Source:
			//用户操作的消息处理
		case notifysource.SystemTask.Get().Source:
			//系统任务的消息处理
		}
	}
}

//告警消息处理
func (action *Handle) handleAlarmNotify(notifyData map[string]interface{}) {
	//数据有效校验
	alarmType := utils.GetStringValueFiled(notifyData, "alarmType")
	alarmLevel := utils.GetIntValueFiled(notifyData, "alarmLevel")
	if len(alarmType) == 0 || alarmLevel == 0 {
		return
	}
	sn := utils.GetStringValueFiled(notifyData, "sn")
	if len(sn) == 0 {
		return
	}
	//设备分组缓存
	deptId, _ := env.Cache.HashGet(constant.DeviceDeptCache, sn)
	if len(deptId) == 0 {
		deptId = constant.DefaultGroupId
	}
	//缓存最新的分组id
	notifyData["deptId"] = deptId
	//查询当前告警数据对应的通知配置信息
	queryConfig := &query.NotifyConfigGetReq{
		Source: notifysource.Alarm.Get().Source,
		Status: able.Enable,
		//Type:   strconv.Itoa(alarmLevel),
		DeptIdEq: deptId,
	}
	//查询数据对应的通知配置
	notifyConfigModel := &models.NotifyConfig{}
	notifyConfigModel.Orm = action.Orm
	configList, err := notifyConfigModel.FindNotifyConfig(queryConfig)
	if err != nil {
		logger.Error(err)
		return
	}
	if len(configList) == 0 {
		return
	}
	//遍历所有的配置
	for _, notifyConfig := range configList {
		//获取通知的模板id配置
		templateIds := notifyConfig.TemplateId
		//不存在直接跳过处理
		if len(notifyConfig.Source) == 0 || len(templateIds) == 0 {
			continue
		}
		//查询通知配置对应的用户信息(告警通知有联系人配置)
		notifyUserList := notifyConfigModel.FindNotifyUserList(notifyConfig)
		//通知模板格式处理		{"1":"SMS_200710187","2":"65d32200-a776-441f-ad65-9745b094781c","3":"","4":"W4wHYmJ21KcXmKjtKPxquL_L-iCGCQohw2KJhW4WNuo"}
		templateConfig := utils.BytesToMap([]byte(templateIds))
		//遍历发送方式的逻辑处理
		for sendWay, templateId := range templateConfig {
			//获取当前的类型
			switch sendWay {
			//告警类型的消息处理
			case sendway.ShortMsg.Get().SendType:
				//短信通知
				handler.SendShortMsgNotify(action.Orm, templateId.(string), notifyData, notifyConfig, notifyUserList)
			case sendway.Email.Get().SendType:
				////邮件通知
				handler.SendEmailNotify(action.Orm, templateId.(string), notifyData, notifyConfig, notifyUserList)
			case sendway.System.Get().SendType:
				////应用内通知
				//handler.SendSystemNotify(notifyData, notifyConfig, notifyUserList)
			case sendway.WeChat.Get().SendType:
				////公众号通知
				//handler.SendWeixinNotify(notifyData, notifyConfig, notifyUserList)
			}
		}
	}
}
